package models

import (
	"time"
	"xiaoshuo/dao"
)

type XiaoHua struct {
	Id        int
	CateId    int
	Content   string
	Status    int
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (XiaoHua) TableName() string {
	return "xiaohua"
}

func GetXiaoHuaById(id int) (XiaoHua, error) {
	var xiaohua XiaoHua
	err := dao.Db.Where("id=?", id).First(&xiaohua).Error
	return xiaohua, err
}

func AddXiaoHua(content string) (int, error) {
	xiaohua := XiaoHua{CateId: 1, Content: content, Status: 1}
	err := dao.Db.Create(&xiaohua).Error
	return xiaohua.Id, err
}

func UpdateXiaoHua(id int, content string) error {
	err := dao.Db.Model(&XiaoHua{}).Where("id=?", id).Update("content", content).Error
	return err
}

func DeleteXiaohua(id int) error {
	err := dao.Db.Model(&XiaoHua{}).Where("id=?", id).Update("status", 0).Error
	return err
}

func GetXiaoHuaList() ([]XiaoHua, error) {
	var xiaohua []XiaoHua
	err := dao.Db.Where("status=?", 1).Find(&xiaohua).Error
	return xiaohua, err
}
